package session;

import hbt.HibernateUtil;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import entities.Falta;
import entities.Gol;

public class HibernateSession {
	
	private static HibernateSession instancia = null;
	@SuppressWarnings("unused")
	private static SessionFactory sf = null;

	public static HibernateSession getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateSession();
		} 
		return instancia;
	}

	public void levantarClubes(String nombreClub) {
		Session session = sf.openSession();
		Query query = session.createQuery("select count(*) as valor from Partido p inner join p.local clubl where clubl.nombre = :nombreClub");
		query.setParameter("nombreClub", nombreClub);
		System.out.println("*************************************");
		System.out.println("Cantidad de partidos de local de " + nombreClub + " : " + query.uniqueResult());
		System.out.println("*************************************");
	}

	public void levantarFaltas() {
		Session session = sf.openSession();
		Query query = session.createQuery("from Falta");
		List<Falta> faltas =(List<Falta>) query.list();
		for (Falta falta : faltas) {
			System.out.println(falta.getTipo());
		}
	}

	public void insertarFalta(Falta falta) {
		try {
			Session session = sf.openSession();
			session.beginTransaction();
			session.getTransaction().commit();
			session.persist(falta);
			session.flush();
			session.close();
			System.out.println("*************************************");
			System.out.println("Falta " + falta.toString());
			System.out.println( "Insertada correctamente");
			System.out.println("*************************************");
		} catch (HibernateException e) {
			System.out.println("*************************************");
			System.out.println("Falta " + falta.toString());
			System.out.println( "No Insertada , error: " + e.getMessage());
			System.out.println("*************************************");
		}
		
		
	}

	public void insertarGol(Gol gol) {
		try {
			Session session = sf.openSession();
			session.beginTransaction();
			session.getTransaction().commit();
			session.persist(gol);
			session.flush();
			session.close();
			System.out.println("*************************************");
			System.out.println("Gol " + gol.toString());
			System.out.println( "Insertada correctamente");
			System.out.println("*************************************");
		} catch (HibernateException e) {
			System.out.println("*************************************");
			System.out.println("Gol " + gol.toString());
			System.out.println( "No Insertada , error: " + e.getMessage());
			System.out.println("*************************************");
		}
	}

	public void hicieronGoles() {
		Session session = sf.openSession();
		Query query = session.createQuery("from Gol");
		List<Gol> goles =(List<Gol>) query.list();
		System.out.println("*************************************");
		System.out.println("Los goles fueron hechos por:");
		for (Gol gol : goles) {
			System.out.println("\t" + gol.getIdGol().getJugador().getNombre());
		}
		System.out.println("*************************************");
		
	}

}

